package com.teach.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teach.entity.ResponseEntity;
import com.teach.entity.TbRole;
import com.teach.entity.TbRolePower;
import com.teach.mapper.TbRolePowerMapper;
import com.teach.mapper.TbUserMapper;
import com.teach.service.TbRoleService;
import com.teach.mapper.TbRoleMapper;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class TbRoleServiceImpl extends ServiceImpl<TbRoleMapper, TbRole>
    implements TbRoleService{
    @Resource
    private TbRoleMapper tbRoleMapper;
    @Resource
    private TbUserMapper tbUserMapper;
    @Resource
    private TbRolePowerMapper tbRolePowerMapper;

    @Override
    public ResponseEntity getAll(TbRole tbRole) {
        QueryWrapper<TbRole> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(tbRole.getName()),"name",tbRole.getName());
        List<TbRole> roleList = tbRoleMapper.selectList(wrapper);
        return new ResponseEntity(roleList,200,"查询成功",true);
    }

    @Override
    public ResponseEntity roleSave(TbRole tbRole,String token) {
        Boolean flag=false;
        List<Map> list=new ArrayList<>();
        //解析token
        Claims claims = Jwts.parser().setSigningKey("huangyujie").parseClaimsJws(token).getBody();
        int uid = Integer.parseInt(claims.getId());
        //根据登录人的id判断是否有权限
        List<Map> mapList = tbUserMapper.selectByUid(uid);
        for (Map map : mapList) {
            //二级名称
            String fname = (String) map.get("fname");
            int indexOf = fname.indexOf("角色管理");
            if(indexOf !=-1){
                //根据登录人的id和二级名称查询三级的名称
                list = tbUserMapper.selectConunt(uid,fname);
            }
        }
        //判断是否有添加的权限
        for (Map map : list) {
            //三级名称
            String gname= (String) map.get("gname");
            //判断是否有添加权限
            if((gname.indexOf("添加"))!=-1){
                flag=true;
            }
        }
        if(flag){
            //添加角色表(主表)
            tbRoleMapper.insert(tbRole);
            //添加角色权限表
            List<TbRolePower> tbRolePowers = tbRole.getTbRolePowers();
            for (TbRolePower tbRolePower : tbRolePowers) {
                tbRolePower.setRoleId(tbRole.getId());
                //添加角色权限表
                tbRolePowerMapper.insert(tbRolePower);
            }
        }
        return new ResponseEntity(null,200,"添加成功",true);
    }

    @Override
    public ResponseEntity updateRole(TbRole tbRole, String token) {
        Boolean flag=false;
        List<Map> list=new ArrayList<>();
        //解析token
        Claims claims = Jwts.parser().setSigningKey("huangyujie").parseClaimsJws(token).getBody();
        int uid = Integer.parseInt(claims.getId());
        //根据登录人的id判断是否有权限
        List<Map> mapList = tbUserMapper.selectByUid(uid);
        for (Map map : mapList) {
            //二级名称
            String fname = (String) map.get("fname");
            int indexOf = fname.indexOf("角色管理");
            if(indexOf !=-1){
                //根据登录人的id和二级名称查询三级的名称
                list = tbUserMapper.selectConunt(uid,fname);
            }
        }
        //判断是否有修改的权限
        for (Map map : list) {
            //三级名称
            String gname= (String) map.get("gname");
            //判断是否有修改权限
            if((gname.indexOf("修改"))!=-1){
                flag=true;
            }
        }
        if(flag){
            //修改的话先删除附表，在添加附表，最后在修改主表
            //根据角色的id去查询附表所对应的数据
            List<TbRolePower> tbRolePowers = tbRolePowerMapper.selectByRid(tbRole.getId());
            for (int i = 0; i < tbRolePowers.size(); i++) {
                TbRolePower tbRolePower = tbRolePowers.get(i);
                tbRolePowerMapper.deleteById(tbRolePower.getId());
            }
            //添加附表
            List<TbRolePower> powerList = tbRole.getTbRolePowers();
            for (TbRolePower tbRolePower : powerList) {
                tbRolePower.setRoleId(tbRole.getId());
                //添加附表
                tbRolePowerMapper.insert(tbRolePower);
            }
            //修改附表
            TbRole role = new TbRole();
            role.setId(tbRole.getId());
            role.setState(tbRole.getState());
            tbRoleMapper.updateById(role);
        }
        return new ResponseEntity(null,200,"修改成功",true);
    }
}




